package org.lsda.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lsda.sched.LsdaScheduler;
import org.lsda.util.Util;
import org.quartz.CronTrigger;
import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;

import com.google.gson.JsonObject;

/**
 * Servlet implementation class CreateTrigger
 */
@WebServlet("/CreateTrigger")
public class CreateTrigger extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CreateTrigger() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String jobName = request.getParameter("JobName");
		String jobGroup = request.getParameter("JobGroup");
		String triggerName = request.getParameter("TriggerName");
		String triggerGroup = request.getParameter("TriggerGroup");
		int schedType = Integer.parseInt(request.getParameter("SchedType"));
		String s = request.getParameter("Start").trim();
		String e = request.getParameter("End").trim();
		Date start = null;
		Date end = null;
		JsonObject json = new JsonObject();
		json.addProperty("Result", "OK");
		try {
			if (!s.equals(""))
				start = sdf.parse(request.getParameter("Start").trim());
			if (!e.equals(""))
				end = sdf.parse(request.getParameter("End").trim());
		} catch (ParseException ex) {
			json.addProperty("Result", "ERROR");
			json.addProperty("Message", ex.getMessage());
			ex.printStackTrace();
			PrintWriter writer = response.getWriter();
//			System.out.println(json.toString());
			writer.write(json.toString());
			return;
		}

		int repeat = Integer.parseInt(request.getParameter("Repeat"));
		int interval = Integer.parseInt(request.getParameter("Interval"));
		String crontab = request.getParameter("Crontab");
		String description = request.getParameter("TriggerDesc");
		Trigger trigger = null;
		try {
			trigger = LsdaScheduler.createTrigger(jobName, jobGroup,
					triggerName, triggerGroup, schedType, start, end, repeat,
					interval, crontab, description);
		} catch (Exception e1) {
			json.addProperty("Result", "ERROR");
			json.addProperty("Message", e1.getMessage());
			e1.printStackTrace();
			PrintWriter writer = response.getWriter();
//			System.out.println(json.toString());
			writer.write(json.toString());
			return;
		}
		JsonObject record = new JsonObject();
		

		if (trigger != null) {
			if (trigger instanceof SimpleTrigger) {
				SimpleTrigger st = (SimpleTrigger) trigger;
				record.addProperty("JobGroup", jobGroup);
				record.addProperty("JobName", jobName);
				record.addProperty("TriggerName", st.getKey().getName());
				record.addProperty("TriggerGroup", st.getKey().getGroup());
				record.addProperty("SchedType", 0);
				record.addProperty("Start", Util.dateString(st.getStartTime()));
				record.addProperty("End", Util.dateString(st.getEndTime()));
				record.addProperty("Repeat", st.getRepeatCount());
				record.addProperty("Interval", st.getRepeatInterval());
				record.addProperty("TriggerDesc", st.getDescription());
			} else if (trigger instanceof CronTrigger) {
				CronTrigger ct = (CronTrigger) trigger;
				record.addProperty("JobGroup", jobGroup);
				record.addProperty("JobName", jobName);
				record.addProperty("TriggerName", ct.getKey().getName());
				record.addProperty("TriggerGroup", ct.getKey().getGroup());
				record.addProperty("SchedType", 1);
				record.addProperty("Start", Util.dateString(ct.getStartTime()));
				record.addProperty("End", Util.dateString(ct.getStartTime()));
				record.addProperty("Crontab", ct.getCronExpression());
				record.addProperty("TriggerDesc", ct.getDescription());
			}

			json.add("Record", record);
		}
		else{
			json.add("Record", null);
			json.addProperty("Result", "Error");
			json.addProperty("Message", "trigger can not be created");
		}

		PrintWriter writer = response.getWriter();
//		System.out.println(json.toString());
		writer.write(json.toString());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
		

	}

}
